﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using PowersourceUIToolkit;
using UCL.Powersource.Helpers;
using UCL.Powersource.ViewModels.Analysis;

namespace UCL.Powersource.Views.Analysis
{
    public abstract class TableViewBase : ContentControl
    {
        protected StatisticViewModelBase VM { get { return (StatisticViewModelBase)(this.DataContext); } }
        protected DataGrid MyTable { get; private set; }

        protected abstract string RowHeaderStyleKey {get;}

        protected DataTable Table
        {
            get
            {
                if (VM == null || VM.StatisticResultsTable == null) return null;

                return VM.StatisticResultsTable;
            }
        }

        public TableViewBase()
        {
            this.Loaded += TableViewBase_Loaded;
            this.Unloaded += TableViewBase_Unloaded;
        }

        void TableViewBase_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (VM != null) VM.Deactivate();
        }

        private T FindAncestorOrSelf<T>(DependencyObject obj) where T : DependencyObject
        {
            while (obj != null)
            {
                T retVal = obj as T;
                if (retVal != null) return retVal;
                obj = VisualTreeHelper.GetParent(obj);
            }
            return null;
        }

        private FrameworkElement FindAncestorOrSelfWithResourceKey(FrameworkElement fe,string resourceKey) 
        {
            while (fe != null)
            {
                if (fe.Resources != null)
                {
                    if (fe.Resources.Contains(resourceKey)) return fe;
                }
                fe = LogicalTreeHelper.GetParent(fe) as FrameworkElement;
            }
            return null;
        }

        //private FindResourceDictionary



        void TableViewBase_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            if (MyTable == null)
            {
                MyTable = new DataGrid();
                this.Content = MyTable;
                MyTable.IsReadOnly = true;
                MyTable.CanUserAddRows = false;
                MyTable.HeadersVisibility = DataGridHeadersVisibility.All;
                MyTable.CanUserResizeRows = true;
                MyTable.AutoGenerateColumns = true;
                MyTable.SelectionUnit = DataGridSelectionUnit.CellOrRowHeader;
                MyTable.Visibility = System.Windows.Visibility.Visible;
                
                //MyTable.ItemsSource = new DataView(VM.StatisticResultsTable);
                Binding bin = new Binding("StatisticResultsTable");
                MyTable.SetBinding(DataGrid.ItemsSourceProperty, bin);

                //var faSWR=FindAncestorOrSelfWithResourceKey(MyTable,RowHeaderStyleKey);
                //if (faSWR != null)
                //{
                //    this.Resources = faSWR.Resources;
                //}
                

                MyTable.SetResourceReference(DataGrid.RowHeaderStyleProperty, RowHeaderStyleKey);
                //if (IoC.GetResourceDictionary().Contains(RowHeaderStyleKey))
                //    MyTable.RowHeaderStyle = (Style)IoC.GetResourceDictionary()[RowHeaderStyleKey];

                MyTable.SetResourceReference(DataGrid.ColumnHeaderStyleProperty, "DatagridColumnHeaderCustomTemplateStyle");
                MyTable.SetResourceReference(DataGrid.CellStyleProperty, "PowersourceCellStyle");
                //if (IoC.GetResourceDictionary().Contains("DatagridColumnHeaderCustomTemplateStyle"))
                //    MyTable.ColumnHeaderStyle = (Style)IoC.GetResourceDictionary()["DatagridColumnHeaderCustomTemplateStyle"];

                MyTable.AutoGeneratedColumns += MyTable_AutoGeneratedColumns;
                MyTable.AutoGeneratingColumn += MyTable_AutoGeneratingColumn;

                
            }

            if (VM != null)
            {
                VM.Activate();
            }
        }

        private void MyTable_AutoGeneratedColumns(object sender, EventArgs e)
        {
            var grid = (DataGrid)sender;
            if (grid.Columns.Count > 0)
                grid.Columns[0].Visibility = System.Windows.Visibility.Hidden;
        }

        private void MyTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            if (e.Column is DataGridTextColumn)
            {
                var source = ((DataGrid)sender).ItemsSource as DataView;
                if (source != null)
                {
                    var sourceTable = source.Table as DataTable;
                    var bin = ((DataGridTextColumn)e.Column).Binding as Binding;
                    if (bin != null)
                    {
                        e.Column.Header = sourceTable.Columns[bin.Path.Path].Caption;

                        if (sourceTable.Columns[bin.Path.Path].ExtendedProperties.Count > 0)
                            bin.StringFormat = (string)sourceTable.Columns[bin.Path.Path].ExtendedProperties[DataTableBuilder.FormatStringKey];
                    }
                }
            }
        }
    }
}
